package com.aibili.contoller.administrator;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.aibili.common.JPushUtil;
import com.aibili.contoller.BaseController;
import com.aibili.pojo.AdminInfo;
import com.aibili.pojo.HelpInfo;
import com.aibili.pojo.NoticeInfo;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.aibili.service.AdminInfoService;
import com.aibili.service.NoticeInfoService;
import com.alibaba.fastjson.JSON;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;

@Controller
@RequestMapping("/administrator")
public class SystemController extends BaseController {

	@Resource
	private NoticeInfoService noticeInfoService;

	@Resource
	private AdminInfoService adminInfoService;

	/**
	 * 
	 * @Description 登录
	 * @param user_name
	 * @param password
	 * @param request
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/login", produces = "application/json; charset=UTF-8")
	public String login(String user_name, String password, HttpServletRequest request, HttpSession session) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		AdminInfo admin = adminInfoService.getAdminByName(user_name);
		if (admin != null) {
			if (admin.getPassword().equals(CommonUtils.MD5(password))) {
				session.setAttribute("admin_name", user_name);
				session.setAttribute("admin_id", admin.getId());
				int type = admin.getType();
				session.setAttribute("adminType", type);
				String permisson = admin.getPermission();
				session.setAttribute("permission", permisson);
				if (!CommonUtils.isEmptyString(permisson)) {
					String[] permissions = permisson.split(",");
					session.setAttribute("permissions", permissions);
					if (permissions.length > 0) {
						switch (permissions[0]) {
						case "1":
							map.put("redurl", "bannerinfo");
							break;
						case "2":
							map.put("redurl", "userinfo");
							break;
						case "3":
							map.put("redurl", "momentinfo");
							break;
						case "4":
							map.put("redurl", "choicenessinfo");
							break;
						case "5":
							map.put("redurl", "exchange");
							break;
						case "6":
							map.put("redurl", "admininfo");
							break;
						default:
							break;
						}
					}
					
				}
				map.put("code", 0);
				
			} else {
				map.put("code", 1);
				map.put("message", "用户名密码错误！");
			}
		} else {
			map.put("code", 1);
			map.put("message", "用户名密码错误！");
		}

		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 退出登录
	 * @param user_name
	 * @param password
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/logout", produces = "application/json; charset=UTF-8")
	public ModelAndView logout(String user_name, String password, HttpSession session) {
		ModelAndView mav = new ModelAndView();

		session.removeAttribute("admin_name");
		session.removeAttribute("admin_id");

		mav.setViewName("administrator/login");

		return mav;
	}

	/**
	 * 
	 * @Description 消息信息查看
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("noticeinfo")
	public ModelAndView noticeinfo(Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<NoticeInfo> results = noticeInfoService.getByParams(pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<NoticeInfo> notices = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", notices);
			mav.addObject("page", page);
		}

		return mav;
	}

	/**
	 * 
	 * @Description 删除系统消息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "noticedelete", produces = "applicatian/json; charset=utf-8")
	public String noticedelete(String id) {
		Map<String, Object> map = new HashMap<String, Object>();

		noticeInfoService.deleteById(CommonUtils.parseLong(id, 0));
		map.put("code", 0);

		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 发布消息
	 * @param title
	 * @param content
	 * @param user_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "publishnotice", produces = "application/json; charset=UTF-8")
	public String publishnotice(String title, String content) {
		Map<String, Object> map = new HashMap<String, Object>();

		NoticeInfo noticeInfo = new NoticeInfo();
		noticeInfo.setContent(content);
		noticeInfo.setPublish_time(new Date());
		noticeInfo.setTitle(title);
		noticeInfo.setUser_id(0);
		JPushUtil jpush = new JPushUtil();
		// 发送给全部用户
		jpush.sendAll(content, null);

		noticeInfoService.addNoticeInfo(noticeInfo);

		map.put("code", 0);
		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 管理员信息查看
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("admininfo")
	public ModelAndView admininfo() {
		ModelAndView mav = new ModelAndView();

		List<AdminInfo> admins = adminInfoService.getAllAdmin();

		mav.addObject("list", admins);
		return mav;
	}

	/**
	 * 
	 * @Description 管理员的新增和修改
	 * @param id
	 * @param user_name
	 * @param password
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/adminedit", produces = "application/json; charset=UTF-8")
	public String adminedit(String id, String add_adminname, String add_password,String type,String permission) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		if (!CommonUtils.isEmptyString(id)) { // 修改密码

			AdminInfo com = adminInfoService.getAdminByNameId(add_adminname, CommonUtils.parseLong(id, 0));
			if (com != null) {
				map.put("code", 1);
				map.put("message", "用户已存在！");
			} else {
				AdminInfo admin = adminInfoService.getAdminById(CommonUtils.parseLong(id, 0));
				if (!CommonUtils.isEmptyString(add_password)) {
					admin.setPassword(CommonUtils.MD5(add_password));
				}if (!CommonUtils.isEmptyString(type)) {
					admin.setType(CommonUtils.parseInt(type, 0));
				}
				if (!CommonUtils.isEmptyString(permission)) {
					admin.setPermission(permission);
				}	
				adminInfoService.updateAdmin(admin);
				map.put("code", 0);

			}

		} else { // 新增

			AdminInfo com = adminInfoService.getAdminByName(add_adminname);
			if (com != null) {
				map.put("code", 1);
				map.put("message", "用户已存在！");
			} else { // admin 存在，没必要判断
				AdminInfo admin = new AdminInfo();

				admin.setUser_name(add_adminname);
				admin.setPassword(CommonUtils.MD5(add_password));
				admin.setType(CommonUtils.parseInt(type, 0));
				admin.setPermission(permission);
				adminInfoService.addAdmin(admin);
				map.put("code", 0);
			}
		}

		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 通过 id 删除管理员
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/admindelete", produces = "application/json; charset=UTF-8")
	public String admindelete(String id) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		if (!CommonUtils.isEmptyString(id)) { // 修改密码

			AdminInfo com = adminInfoService.getAdminById(CommonUtils.parseLong(id, 0));
			if (com != null && !com.getUser_name().equals("admin")) {
				map.put("code", 0);
				adminInfoService.deleteAdminById(CommonUtils.parseLong(id, 0));
			}
		}

		return JSON.toJSONString(map);
	}

	@RequestMapping("helpinfo")
	public ModelAndView helpinfo(Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<HelpInfo> results = noticeInfoService.getHelplist(pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<HelpInfo> helplist = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", helplist);
			mav.addObject("page", page);
		}

		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value = "publishhelp", produces = "application/json; charset=UTF-8")
	public String publishhelp(String title, String content) {
		Map<String, Object> map = new HashMap<String, Object>();
		HelpInfo helpInfo = new HelpInfo();
		helpInfo.setQuestion(title);
		helpInfo.setResponse(content);
		helpInfo.setAddtime(new Date());
		noticeInfoService.addHelpInfo(helpInfo);
		map.put("code", 0);
		return JSON.toJSONString(map);
	}
	@ResponseBody
	@RequestMapping(value = "helpdelete", produces = "applicatian/json; charset=utf-8")
	public String helpdelete(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		noticeInfoService.deleteHelpById(CommonUtils.parseLong(id, 0));
		map.put("code", 0);
		return JSON.toJSONString(map);
	}
}
